package com.example.demo.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.user.entity.UTypeClassification;
import com.example.demo.user.service.UTypeClassificationService;
import com.example.demo.user.vo.UTypeClassificationVo;
import com.example.demo.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务账号类目表; 前端控制器
 * </p>
 *
 * @author 涛
 * @since 2024-08-01
 */
@RestController
@RequestMapping("/uTypeClassification")
@Tag(name = "uTypeClassification", description = "大账号类型维护模块")
public class UTypeClassificationController {

    @Autowired
    private UTypeClassificationService uTypeClassificationService;

    /**
     * 获取大账号类型模块列表
     */
    @GetMapping("/getUTypeList")
    @Operation(summary = "获取大账号类型列表接口", description = "获取大账号类型列表接口")
    public Result<?> getUTypeList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                 @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                 UTypeClassification uType){
        String category = uType.getCategory();
        uType.setCategory(null);
        Page<UTypeClassification> page = new Page<>(current,size);
        QueryWrapper<UTypeClassification> queryWrapper = new QueryWrapper<>(uType);
        if(StringUtils.isNotEmpty(category)){
            queryWrapper.like("category",category);
        }
        queryWrapper.select("u_type_classification.*,(select count(*) from t_note_manage note inner join t_note_likes l on note.ID = l.TID where note.ACCOUNT = u_type_classification.category_code) as likeNum," +
                "(select count(*) from u_account_collect uac where uac.category_code = u_type_classification.category_code) as collectNum, if((select id from u_account_collect uac where uac.category_code = u_type_classification.category_code and uac.uid = "+uType.getUid()+") is null,false,true)izCollectAccount");
        return Result.ok(uTypeClassificationService.page(page,queryWrapper));
    }

    /**
     * 根据ID查询大账号类型
     */
    @GetMapping("/getUTypeById")
    @Operation(summary = "根据id查询大账号类型明细", description = "根据id查询大账号类型明细")
    public Result<?> getUTypeById(Integer id){
        return Result.ok(uTypeClassificationService.getById(id));
    }

    /**
     * 修改大账号类型
     */
    @PutMapping("/updateUType")
    @Operation(summary = "根据id修改大账号类型明细", description = "根据id修改大账号类型明细")
    @Transactional
    public Result<?> updateUType(@RequestBody UTypeClassification uType){
        return Result.ok(uTypeClassificationService.updateById(uType));
    }

    /**
     * 新增大账号类型
     */
    @PostMapping("/addUType")
    @Operation(summary = "新增大账号类型明细", description = "新增大账号类型明细")
    public Result<?> addUType(@RequestBody UTypeClassification uType){
        return Result.ok(uTypeClassificationService.save(uType));
    }

    /**
     * 删除大账号类型
     */
    @DeleteMapping("/deleteUTypeById")
    @Operation(summary = "删除大账号类型", description = "删除大账号类型")
    @Transactional
    public Result<?> deleteUTypeById(Integer id){
        return Result.ok(uTypeClassificationService.removeById(id));
    }

    /**
     * 查看账号收藏列表
     */
    @GetMapping("/getAccountCollectionList")
    @Operation(summary = "查看账号收藏列表", description = "查看账号收藏列表")
    @Transactional
    public Result<?> getAccountCollectionList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                              @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                              Integer id){
        Page<UTypeClassificationVo> page = new Page<>(current,size);
        return uTypeClassificationService.getAccountCollectionList(page,id);
    }

    @DeleteMapping("/batch/{ids}")
    @Operation(summary = "批量删除", description = "批量删除")
    public Result<Void> deleteDictType(@PathVariable String ids) {
        List<String> idArray = Arrays.asList(ids.split(","));
        uTypeClassificationService.removeBatchByIds(idArray);
        return Result.ok();
    }

    @GetMapping("/getAgent")
    @Operation(summary = "ceshi", description = "ceshi")
    public Result<?> getAgent(){
        String text = "肺热产生的原因如下：1、有可能多半是装的。2、个人体制问题";
        return Result.ok(text);
    }

    @GetMapping("/getAgent2")
    @Operation(summary = "ceshi", description = "ceshi")
    public Result<?> getAgent2(){
        String text = "治疗肺热的方法如下：1.一般打一顿就好了。2、去医院积极接收治疗";
        return Result.ok(text);
    }
}

